package com.speily.cloud.mall.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * 通用响应对象
 *
 * @author SPL
 * @since 2020-02-04 11:52
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class AjaxResult {

    public AjaxResult(RespEnum e) {
        this.code = e.getCode();
        this.message = e.getMsg();
    }

    public AjaxResult(RespEnum e, Object data) {
        this.code = e.getCode();
        this.message = e.getMsg();
        this.result = data;
    }

    /**
     * 响应编号
     */
    private int code;
    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应数据
     */
    private Object result;

    public AjaxResult(int code) {
        this.code = code;
    }

    public AjaxResult(int code, String msg) {
        this.code = code;
        this.message = msg;
    }


    /**
     * 成功
     *
     * @return
     */
    public static AjaxResult success() {
        return success(null);
    }

    /**
     * 成功
     *
     * @param data
     * @return
     */
    public static AjaxResult success(Object data) {
        return new AjaxResult(RespEnum.OK, data);
    }

    public static AjaxResult successFlow() {
        return new AjaxResult(0);
    }

    public static AjaxResult rollbackFlow(String msg) {
        return new AjaxResult(1,msg);
    }

    /**
     * 失败
     */
    public static AjaxResult error(RespEnum e) {
        return new AjaxResult(e);
    }

    /**
     * 失败
     */
    public static AjaxResult error(RespEnum e, String msg) {
        return new AjaxResult().setCode(e.getCode()).setMessage(msg);
    }

    /**
     * 失败
     */
    public static AjaxResult error(String msg) {
        return new AjaxResult().setCode(RespEnum.ERROR.getCode()).setMessage(msg);
    }

    /**
     * 失败
     */
    public static AjaxResult error(int code, String msg) {
        return new AjaxResult().setCode(code).setMessage(msg);
    }

}
